package menu;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
import java.util.Vector;

import core.GameOptions;
import core.gamemodes.GameMode;
import core.mouse.Clickable;
import core.mouse.OBCMouseListener;

public class OptionButton implements Clickable {
	
	public enum ReactionType{MAXPLAYERS, MAP, GAMEMODE}
	
	public final static Font optionFont = new Font("Monospaced", Font.PLAIN, 20);
	public static FontMetrics fm;
	
	protected Rectangle bounds;
	protected String optionName = "NAN";
	protected ReactionType reaction;
	protected List<String> options;
	
	
	protected int nameWidth = 150;
	
	protected int highlightedOption = 0;
	protected int partWidth;
	
	public OptionButton(String optionName, ReactionType reaction, List<String> options, int x, int y, int width, int startOption){
		this.options = options;
		this.optionName = optionName;
		this.reaction = reaction;
		bounds = new Rectangle(x, y, width, 25);
		
		if(options.size()>0)
			partWidth = (width-nameWidth)/options.size();
		else
			partWidth = 100;
		
		OBCMouseListener.add(this);
		
		trigger(startOption);
	}

	@Override
	public Rectangle getBounds() {
		return bounds;
	}
	
	@Override
	public void paint(Graphics g){
		g.setFont(optionFont);
		fm = g.getFontMetrics();
		g.setColor(Color.lightGray);
		g.fillRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, bounds.height, bounds.height);
		g.setColor(Color.black);
		g.drawString(optionName, bounds.x+10, bounds.y+bounds.height-5);
		for(int i = 0; i < options.size(); i++){
			if(highlightedOption == i){
				g.setColor(Color.red);
				g.fillRoundRect(bounds.x+nameWidth+partWidth*i, bounds.y, partWidth, bounds.height, bounds.height, bounds.height);
			}
			g.setColor(Color.gray);
			g.drawRoundRect(bounds.x+nameWidth+partWidth*i, bounds.y, partWidth, bounds.height-1, bounds.height, bounds.height);
			
			g.setColor(Color.black);
			int x = bounds.x+nameWidth+partWidth*i + partWidth/2 - fm.stringWidth(options.get(i))/2;
			g.drawString(options.get(i), x, bounds.y+bounds.height-5);
		}
	}

	@Override
	public void click(int x, int y) {
		x = x - nameWidth-bounds.x;
		if(x >= 0){
			int optionNumber = x/partWidth;
			trigger(optionNumber);
		}
		//System.out.println("Trigger "+x+" "+optionNumber);
		
	}
	
	public void trigger(int optionNumber){
		if(optionNumber >= 0 && optionNumber < options.size()){
			highlightedOption = optionNumber;	
			
			switch(reaction){
			case MAXPLAYERS:
				GameOptions.expectedMaxPlayers = Integer.parseInt(options.get(optionNumber));
				break;
			case MAP:
				GameOptions.mapName = options.get(optionNumber);
				break;
			case GAMEMODE:
				for(GameMode gm : GameOptions.getAvailableGameModes()){
					if(gm.getName().equals(options.get(optionNumber)))
						GameOptions.selectedGameMode = gm;
				}				
				break;
			}
			
		}
	}

}
